home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / icon cache demo / icon cache.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.5 KB  |  242 lines

  1. /*
  2.     File:        icon cache.c
  3.  
  4.     Contains:    This sample demonstrates the use of an icon cache
  5.                 to limit the search for icon resource to one resource
  6.                 file. It does this by installing an icon getter function
  7.                 into the cache which calls Get1(Ind)Resource instead of the
  8.                 usual GetResource.
  9.         
  10.                 The application is meant to display the first
  11.                 suite produced by an indexed resource call. There's nothing
  12.                 stopping you from calling Get1Resource or anything else
  13.                 which might produce a handle to a member of an icon suite.
  14.         
  15.                 There's also some jiggery-pokery having to do with my
  16.                 distaste for purgeable handles; see below for details.
  17.  
  18.     Written by: Pete Gontier    
  19.  
  20.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  21.  
  22.                 You may incorporate this Apple sample source code into your program(s) without
  23.                 restriction. This Apple sample source code has been provided "AS IS" and the
  24.                 responsibility for its operation is yours. You are not permitted to redistribute
  25.                 this Apple sample source code as "Apple sample source code" after having made
  26.                 changes. If you're going to re-distribute the source, we require that you make
  27.                 it clear in the source that the code was descended from Apple sample source
  28.                 code, but that you've made changes.
  29.  
  30.     Change History (most recent first):
  31.                 8/6/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  32.                 
  33.  
  34. */
  35.  
  36. #define OLDROUTINELOCATIONS        0
  37. #define OLDROUTINENAMES            0
  38. #define SystemSevenOrLater        1
  39.  
  40. #ifndef __FONTS__
  41. #    include <Fonts.h>
  42. #endif
  43.  
  44. #ifndef __DIALOGS__
  45. #    include <Dialogs.h>
  46. #endif
  47.  
  48. #ifndef __RESOURCES__
  49. #    include <Resources.h>
  50. #endif
  51.  
  52. #ifndef __STANDARDFILE__
  53. #    include <StandardFile.h>
  54. #endif
  55.  
  56. #ifndef __ICONS__
  57. #    include <Icons.h>
  58. #endif
  59.  
  60. static pascal Handle MyIconGetter (ResType rt, void *)
  61. {
  62.     //
  63.     //    DisposeIconSuite assumes resources are purgeable and doesn't
  64.     //    release them. Since we would rather the memory be freed,
  65.     //    we have our icon getter function ('MyIconGetter') detach
  66.     //    each resource it gets. This turns off the resource bit for
  67.     //    the handle and clues DisposeIconSuite it should call DisposeHandle.
  68.     //
  69.  
  70.     Handle iconH = Get1IndResource (rt,1);
  71.     if (!iconH || ResError ( ))
  72.         return nil;
  73.  
  74.     DetachResource (iconH);
  75.     if (ResError ( ))
  76.     {
  77.         ReleaseResource (iconH);
  78.         return nil;
  79.     }
  80.  
  81.     return iconH;
  82. }
  83.  
  84. static pascal void Plot1IndIconSuite (Rect *rect)
  85. {
  86.     Handle iconCacheH;
  87.  
  88.     static IconGetterUPP iconGetterUPP;
  89.  
  90.     if (!iconGetterUPP)
  91.         iconGetterUPP = NewIconGetterProc (MyIconGetter);
  92.     if (!iconGetterUPP)
  93.         return;
  94.     if (!MakeIconCache (&iconCacheH,iconGetterUPP,nil))
  95.     {
  96.         PlotIconSuite (rect,kAlignNone,kTransformNone,iconCacheH);
  97.  
  98.         //
  99.         //    DisposeIconSuite assumes resources are purgeable and doesn't
  100.         //    release them. Since we would rather the memory be freed,
  101.         //    we have our icon getter function ('MyIconGetter') detach
  102.         //    each resource it gets. This turns off the resource bit for
  103.         //    the handle and clues DisposeIconSuite it should call DisposeHandle.
  104.         //
  105.  
  106.         DisposeIconSuite (iconCacheH,true);
  107.     }
  108. }
  109.  
  110. //////////////////////////////////////////////////////////////////////
  111. //
  112. //    Below please find the usual sort of application boilerplate.
  113. //
  114. //////////////////////////////////////////////////////////////////////
  115.  
  116. static Boolean gQuitting;
  117.  
  118. static pascal OSErr InitMac (void)
  119. {
  120.     MaxApplZone ( );
  121.     InitGraf (&(qd.thePort));
  122.     InitFonts ( );
  123.     InitWindows ( );
  124.     InitMenus ( );
  125.     TEInit ( );
  126.     InitDialogs (nil);
  127.  
  128.     return noErr;
  129. }
  130.  
  131. static pascal void IconUserItem (WindowRef dlgRef, short itemNo)
  132. {
  133.     short crf = CurResFile ( );
  134.     short iType; Handle iHandle; Rect iRect;
  135.     GetDialogItem (dlgRef,itemNo,&iType,&iHandle,&iRect);
  136.     UseResFile (GetWRefCon (dlgRef));
  137.     if (!ResError ( ))
  138.     {
  139.         Plot1IndIconSuite (&iRect);
  140.         UseResFile (crf);
  141.     }
  142. }
  143.  
  144. static pascal void GrabIcon (void)
  145. {
  146.     StandardFileReply sfr;
  147.     StandardGetFile (nil,-1,nil,&sfr);
  148.     if (sfr.sfGood)
  149.     {
  150.         short crf = CurResFile ( );
  151.         short resRefNum = FSpOpenResFile (&(sfr.sfFile),fsRdPerm);
  152.         if (resRefNum != -1)
  153.         {
  154.             DialogRef dlgRef = nil;
  155.  
  156.             UseResFile (crf);
  157.  
  158.             dlgRef = GetNewDialog (128,nil,(WindowRef)-1);
  159.             if (dlgRef)
  160.             {
  161.                 short itemHit;
  162.                 short iType; Handle iHandle; Rect iRect;
  163.                 static UserItemUPP userItemUPP;
  164.  
  165.                 if (!userItemUPP)
  166.                     userItemUPP = NewUserItemProc (IconUserItem);
  167.                 if (userItemUPP)
  168.                 {
  169.                     GetDialogItem (dlgRef,2,&iType,&iHandle,&iRect);
  170.                     SetDialogItem (dlgRef,2,iType,(Handle)userItemUPP,&iRect);
  171.                     SetWRefCon (dlgRef,resRefNum);
  172.  
  173.                     ShowWindow (dlgRef);
  174.                     ModalDialog ( NewModalFilterProc(nil),&itemHit);
  175.                 }
  176.                 DisposeDialog (dlgRef);
  177.             }
  178.             
  179.             CloseResFile (resRefNum);
  180.         }
  181.     }
  182. }
  183.  
  184. static pascal void Command (long ms)
  185. {
  186.     short    menuID = ms >> 16,
  187.             menuItem = ms;
  188.  
  189.     if (menuID == 129)
  190.     {
  191.         if (menuItem == 2)
  192.             gQuitting = true;
  193.         else if (menuItem == 1)
  194.             GrabIcon ( );
  195.     }
  196. }
  197.  
  198. static pascal void HandleEvent (const EventRecord *eventP)
  199. {
  200.     if (eventP->what == mouseDown)
  201.     {
  202.         WindowRef whichWindow;
  203.         short partCode = FindWindow (eventP->where, &whichWindow);
  204.         if (partCode == inMenuBar)
  205.         {
  206.             long ms = MenuSelect (eventP->where);
  207.             if (ms) Command (ms);
  208.             HiliteMenu (0);
  209.         }
  210.     }
  211. }
  212.  
  213. static pascal Boolean SetUpMenuBar (void)
  214. {
  215.     Boolean result = false;
  216.     Handle mBar = GetNewMBar (128);
  217.     if (!ResError ( ) && mBar)
  218.     {
  219.         SetMenuBar (mBar);
  220.         AppendResMenu (GetMenuHandle (130), 'DRVR');
  221.         DrawMenuBar ( );
  222.         result = true;
  223.         ReleaseResource (mBar);
  224.     }
  225.     return result;
  226. }
  227.  
  228. void main (void)
  229. {
  230.     if (!InitMac ( ) && SetUpMenuBar ( ))
  231.     {    
  232.         do
  233.         {
  234.             EventRecord event;
  235.             InitCursor ( );
  236.             WaitNextEvent (everyEvent, &event, -1, nil);
  237.             HandleEvent (&event);
  238.         }
  239.         while (!gQuitting);
  240.     }
  241. }
  242.